layui.use(['form', 'table','layuimini','element', 'laydate'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini,
        laydate = layui.laydate;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoMarkDate = {
        id: 'BoMarkDateTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'title', title: '标题', align: 'center'},
            {field: 'markDateValue', title: '日期', width: 110, align: 'center'},
            {field: 'remarkType', title: '提醒类型', width: 100, align: 'center',
                templet: function (object) {
                    if(object.remarkType == 0){
                        return '<span class="layui-badge layui-bg-green">生日提醒</span>';
                    }else if(object.remarkType == 1){
                        return '<span class="layui-badge layui-bg-orange">纪念节日</span>';
                    }else if(object.remarkType == 2){
                        return '<span class="layui-badge layui-bg-blue">普通节日</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-cyan">备忘事项</span>';
                    }
                }
            },
            {field: 'dateType', title: '日期类型', width: 95, align: 'center',
                templet: function (object) {
                    if(object.dateType == 0){
                        return '<span class="layui-badge layui-bg-green">公历</span>';
                    }else{
                        return '<span class="layui-badge">农历</span>';
                    }
                }
            },
            {field: 'sendTime', title: '发送次数', width: 100, align: 'center',
                templet: function (object) {
                    if(object.sendTime == 0){
                        return '<span class="layui-badge layui-bg-green">五天三次</span>';
                    }else if(object.sendTime == 1){
                        return '<span class="layui-badge layui-bg-blue">五天两次</span>';
                    }else if(object.sendTime == 2){
                        return '<span class="layui-badge layui-bg-orange">当天发送</span>';
                    }
                }
            },
            {field: 'runTimeType', title: '提醒时间类型', width: 110, align: 'center',
                templet: function (object) {
                    if(object.runTimeType == 0){
                        return '<span class="layui-badge layui-bg-green">早上八点</span>';
                    }else if(object.runTimeType == 1){
                        return '<span class="layui-badge layui-bg-blue">早上九点</span>';
                    }else if(object.runTimeType == 2){
                        return '<span class="layui-badge layui-bg-orange">下午六点</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-cyan">晚上八点</span>';
                    }
                }
            },
            {field: 'personName', title: '本人昵称', width: 100, align: 'center'},
            {field: 'personPhone', title: '本人提醒手机号', width: 130, align: 'center'},
            {field: 'topic', title: '本人提醒主题(节日名称)', align: 'center'},
            {field: 'remindPhone', title: '额外提醒手机号', align: 'center'},
            {field: 'otherTopic', title: '额外提醒主题(节日名称)', align: 'center'},
            {field: 'isOpen', title: '开启提醒', width: 95, align: 'center',
                templet: function (object) {
                    if(object.isOpen == 1){
                        return '<span class="layui-badge layui-bg-green">是</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-cyan">否</span>';
                    }
                }
            },
            {field: 'remark', title: '备注', align: 'center',
                templet: function (object) {
                    if(object.remark == '' || object.remark == undefined || object.remark == null){
                        return '-';
                    }else{
                        return object.remark;
                    }
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoMarkDate.id,
        url: '/web/boMarkDate/recycleList',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    /**
     * 点击彻底删除按钮
     * @param id
     */
    BoMarkDate.thoroughlyDelete = function(id){
        layer.open({
            title: '彻底删除纪念日'
            ,content: '是否确定彻底删除该纪念日？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boMarkDate/thoroughlyDelete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoMarkDate.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    /**
     * 点击恢复按钮
     * @param id
     */
    BoMarkDate.restore = function(id){
        layer.open({
            title: '恢复纪念日'
            ,content: '是否确定恢复该纪念日？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boMarkDate/restore';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoMarkDate.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };



    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoMarkDate.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'thoroughlyDelete') {
            BoMarkDate.thoroughlyDelete(data.id);
        }else if(layEvent === 'restore'){
            BoMarkDate.restore(data.id);
        }
    });

});